package com.yanqu.road.server.cmd.activity.gold;

import com.yanqu.road.entity.activity.ActivityDisperseGoldResult;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.logic.pb.ActivityDisperseGoldPb;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityDisperseGoldProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityTrystModule;
import com.yanqu.road.server.gameplayer.module.activity.holiday.HolidayActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.huashigang.HuaShiGangModule;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.gameplayer.module.activity.throwingPot.ThrowingPotModule;
import com.yanqu.road.server.gameplayer.module.activity.treasure.TreasureActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.winterLove.WinterLoveModule;
import com.yanqu.road.server.gameplayer.module.activity.yearMaster.YearMasterModule;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_ACTIVITY_DISPERSE_GOLD_REQ, desc = "送财请求")
public class ActivityDisperseGoldCmd extends AbstractCommand {

    private int activityId;

    private String manifesto;

    ActivityInfo activityInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityDisperseGoldProto.ActivityDisperseGoldReqMsg reqMsg = ActivityDisperseGoldProto.ActivityDisperseGoldReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        manifesto = reqMsg.getManifesto();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityDisperseGoldResult result = null;
        activityInfo = NormalActivityMgr.getActivityInfo(activityId);     //送糖与活动期限有关
        if (activityInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        //花石纲不需要manifesto、再加个皇陵
        if(activityInfo.getType() != eActivityType.CrossHuaShiGangActivity.getValue()
                && activityInfo.getType() != eActivityType.Tomb.getValue()
        ){
            //编辑限制
            GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_ACTIVITY_DISPERSE_GOLD_REQ);
            if(null != gameEditLimit){
                long nowTime = System.currentTimeMillis();
                if(nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()){
                    String replaceStr = "祝掌柜们财源广进！";
                    if(!StringUtils.isNullOrEmpty(gameEditLimit.getReplaceContent())){
                        replaceStr = gameEditLimit.getReplaceContent();
                    }
                    manifesto = replaceStr;
                }
            }

            if(ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_ACTIVITY_DISPERSE_GOLD_REQ)) {
                ret = GameErrorCode.E_SYSTEM_NO_OPEN;
                return;
            }

            if(ForbiddenWordMgr.isForbidden(manifesto)){
                ret = GameErrorCode.E_DISPERSEGOLD_FORBIDDEN;
                return;
            }
        }

        // 灵猫活动送糖
        if (activityInfo.getType() == eActivityType.CrossHolidayActivity.getValue()) {
            result = player.getModule(HolidayActivityModule.class).disperseGold(activityInfo, manifesto);
        }
        // 投壶飞箭送礼
        if (activityInfo.getType() == eActivityType.ThrowingPotActivity.getValue()) {
            result = player.getModule(ThrowingPotModule.class).disperseGold(activityInfo, manifesto);
        }
        // 瑞兽降福送礼
        if (activityInfo.getType() == eActivityType.YearMasterActivity.getValue()) {
            result = player.getModule(YearMasterModule.class).disperseGold(activityInfo, manifesto);
        }
        // 灵狐聚宝送礼
        if (activityInfo.getType() == eActivityType.TreasureActivity.getValue()) {
            result = player.getModule(TreasureActivityModule.class).disperseGold(activityInfo, manifesto);
        }
        // 服装活动散财
        if (activityInfo.getType() == eActivityType.TrystActivity.getValue()) {
            result = player.getModule(ActivityTrystModule.class).disperseGold(activityInfo, manifesto);
        }
        // 花石纲
        if (activityInfo.getType() == eActivityType.CrossHuaShiGangActivity.getValue()) {
            result = player.getModule(HuaShiGangModule.class).disperseGold(activityInfo, manifesto);
        }
        // 冬季恋歌
        if (activityInfo.getType() == eActivityType.WinterLoveActivity.getValue()) {
            result = player.getModule(WinterLoveModule.class).disperseGold(activityInfo, manifesto);
        }
        // 水浒叶子牌
        if (activityInfo.getType() == eActivityType.ShuiHuYeZiCardActivity.getValue()) {
            result = player.getModule(ShuiHuYeZiCardModule.class).disperseGold(activityInfo, manifesto);
        }

        if (activityInfo.getType() == eActivityType.Tomb.getValue()) {
            result = player.getModule(TombModule.class).disperseGold(activityInfo, manifesto);
        }


        ret = result != null ? result.getRet() : GameErrorCode.E_ACTIVITY_NO_FOUND;
        if (ret == 0) {
            // 活动日志
            LogMgr.addLogActivityDisperseGold(player.getUserId(), activityId);
            // 发送财
            ActivityDisperseGoldProto.CrossActivityDisperseGoldRespMsg.Builder disperseRespMsg = ActivityDisperseGoldProto.CrossActivityDisperseGoldRespMsg.newBuilder();
            disperseRespMsg.setActivityId(activityId);
            ActivityDisperseGoldProto.ActivityDisperseGoldInfoTemp.Builder disperseGoldTempBuilder = ActivityDisperseGoldPb.parseToDisperseGoldInfoTemp(result.getGoldInfo());
            disperseRespMsg.setGoldInfo(disperseGoldTempBuilder);
            disperseRespMsg.setGroupId(result.getGoldInfo().getGroupId());
            YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_DISPERSE_GOLD_REQ_PUSH, disperseRespMsg);
            GamePlayerMgr.sendPacket(0, pbMessage);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        ActivityDisperseGoldProto.ActivityDisperseGoldRespMsg.Builder respMsg = ActivityDisperseGoldProto.ActivityDisperseGoldRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setRet(ret);
        if (activityInfo != null) {
            respMsg.setType(activityInfo.getType());
        }
        player.sendPacket(Protocol.U_ACTIVITY_DISPERSE_GOLD_REQ, respMsg);
    }
}
